package com.wego.controller;

import com.wego.bean.StateBean;
import com.wego.bean.TableBean;
import com.wego.common.minio.MinioUtil;
import com.wego.entity.domain.Goods;
import com.wego.service.GoodsService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @className: DispatcherController
 * @description: 通用控制类
 * @author: Li Chuanwei
 * @date: 2023/07/10 14:17
 * @Company: Copyright [日期] by [作者或个人]
 **/
@Controller
public class DispatcherController {
    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/fileUpload")
    public @ResponseBody StateBean fileUpload(MultipartFile file) {
        final String s = minioUtil.putObject(file, file.getOriginalFilename());
        System.out.println(s);
        return StateBean.builder()
                .code(200)
                .message("成功")
                .build();
    }


    @PostMapping("goods/fileUpload")
    public @ResponseBody StateBean goodsFileUpload(MultipartFile file) {
        String filename = file.getOriginalFilename();
        int dotIndex = filename.lastIndexOf(".");
        String filenameWithoutExtension = filename.substring(0, dotIndex);
        final String s = minioUtil.putObject(file, "goods", filenameWithoutExtension);
        System.out.println(s);
        return StateBean.builder()
                .code(200)
                .message("成功")
                .build();
    }

    @PostMapping("goods/imgsUpload")
    public @ResponseBody StateBean goodsImgsFileUpload(@RequestParam("file") MultipartFile[] files) {
        for (MultipartFile file : files) {
            String filename = file.getOriginalFilename();
            int dotIndex = filename.lastIndexOf(".");
            String filenameWithoutExtension =filename.substring(0, dotIndex);
            final String s = minioUtil.putObject(file, "goods", filenameWithoutExtension);
            System.out.println(s);
        }
        return StateBean.builder()
                .code(200)
                .message("成功")
                .build();
    }

    @PostMapping("carousel/fileUpload")
    public @ResponseBody StateBean carouselFileUpload(MultipartFile file) {
        String filename = file.getOriginalFilename();
        int dotIndex = filename.lastIndexOf(".");
        String filenameWithoutExtension = filename.substring(0, dotIndex);
        final String s = minioUtil.putObject(file, "carousel", filenameWithoutExtension);
        System.out.println(s);
        return StateBean.builder()
                .code(200)
                .message("成功")
                .build();
    }

    @GetMapping("/mto_{xxx}")
    public String goToPage(@PathVariable("xxx") String page, HttpServletRequest request,
                           HttpServletResponse response) throws IOException {
        //拦截一下
        HttpSession session = request.getSession();
        if(!page.equals("login") && session.getAttribute("admin") == null){
            //跳转动作
            return "manager/unauthorized";
        }

        String url = "manager/" + page;
        return url;
    }

    @GetMapping("/uto_{xxx}")
    public String goToUserPage(@PathVariable("xxx") String page) {
        String url = "user/" + page;
        return url;
    }

    @GetMapping("/setPassword")
    public String setPassword() {
        return "setPassword";
    }

    private void fun(Set<String> set, String str, String keywords) {
        if (str != null && str.contains(keywords)) {
            String tmp = str.substring(str.indexOf(keywords));
            tmp = tmp.substring(0, Math.min(tmp.length(), 5));
            if (tmp != null) {
                set.add(tmp);
            }
        }
    }

    @ResponseBody
    @GetMapping("/autoComplete")
    TableBean<Goods> autoComplete(String val) {
        List<Goods> goodsList = goodsService.selectRecommend(val);
        TableBean<Goods> tableBean = new TableBean<>();
        tableBean.setCode(0);
        tableBean.setMsg("");
        tableBean.setData(goodsList);
        Long count = (long) goodsList.size();
        tableBean.setCount(count);
        return tableBean;
    }


}
